package org.example.volunteerend.mapper;

import java.time.LocalDateTime;
import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.example.volunteerend.entity.AdminOperationLog;

@Mapper
public interface AdminOperationLogMapper {
    
    /**
     * 添加管理员操作日志
     */
    @Insert("INSERT INTO admin_operation_logs (admin_id, operation_type, operation_description, ip_address, create_time) " +
            "VALUES (#{log.adminId}, #{log.operationType}, #{log.operationDescription}, #{log.ipAddress}, NOW())")
    @Options(useGeneratedKeys = true, keyProperty = "log.logId")
    void insert(@Param("log") AdminOperationLog log);
    
    /**
     * 查询管理员操作日志
     */
    @Select("<script>" +
            "SELECT l.*, u.username as admin_name FROM admin_operation_logs l " +
            "LEFT JOIN users u ON l.admin_id = u.user_id " +
            "WHERE 1=1 " +
            "<if test='adminId != null'>" +
            "  AND l.admin_id = #{adminId} " +
            "</if>" +
            "<if test='startTime != null'>" +
            "  AND l.create_time &gt;= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "  AND l.create_time &lt;= #{endTime} " +
            "</if>" +
            "<if test='operationType != null and operationType != \"\"'>" +
            "  AND l.operation_type = #{operationType} " +
            "</if>" +
            "ORDER BY l.create_time DESC " +
            "LIMIT #{offset}, #{pageSize}" +
            "</script>")
    List<AdminOperationLog> findAdminOperationLogs(
            @Param("adminId") Integer adminId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("operationType") String operationType,
            @Param("offset") Integer offset,
            @Param("pageSize") Integer pageSize);
    
    /**
     * 统计符合条件的管理员操作日志数量
     */
    @Select("<script>" +
            "SELECT COUNT(*) FROM admin_operation_logs l " +
            "WHERE 1=1 " +
            "<if test='adminId != null'>" +
            "  AND l.admin_id = #{adminId} " +
            "</if>" +
            "<if test='startTime != null'>" +
            "  AND l.create_time &gt;= #{startTime} " +
            "</if>" +
            "<if test='endTime != null'>" +
            "  AND l.create_time &lt;= #{endTime} " +
            "</if>" +
            "<if test='operationType != null and operationType != \"\"'>" +
            "  AND l.operation_type = #{operationType} " +
            "</if>" +
            "</script>")
    int countAdminOperationLogs(
            @Param("adminId") Integer adminId,
            @Param("startTime") LocalDateTime startTime,
            @Param("endTime") LocalDateTime endTime,
            @Param("operationType") String operationType);
} 